package com.hd.action;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.xwork.StringUtils;

import com.hd.dao.impl.BaseAction;



public class LoginAction extends BaseAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public void login() throws Exception{
		Map<String,Object> loginMap = new HashMap<String,Object>();
		if(validLogin()>0){
			loginMap.put("status", false);
			loginMap.put("result", validLogin());
			responseToJsonObject(loginMap);
			return ;
		}
		if(!checkLogin()){
			loginMap.put("status", false);
			loginMap.put("result", 3);
			responseToJsonObject(loginMap);
			return ;
		}
		loginMap.put("status", true);
		loginMap.put("result", 0);
		loginMap.put("url", getRequest( ).getContextPath()+"/index.jsp");
		responseToJsonObject(loginMap);
		return ;
	}
	
	public boolean checkSeccode(String seccode){
		String sessionCode = getSession("seccode");
		return StringUtils.equalsIgnoreCase(sessionCode, seccode);
	}
	public boolean checkLogin() throws Exception{
		String sql = "select * from login where username="+username;
		Map<String,Object> map = getData(sql);
		String password = (String)map.get("password");
		return StringUtils.equals(password, pwd);
	}
	public int validLogin(){
		if(StringUtils.isBlank(username)||StringUtils.isBlank(pwd)){
			return 1;
		}
		if(StringUtils.isBlank(seccode)){
			return 2;
		}
		if(!checkSeccode(seccode)){
			return 2;
		}
		return 0;
	}
	
	 
	
	private String username;
	private String pwd;
	private String seccode;
	
	public String getSeccode() {
		return seccode;
	}
	public void setSeccode(String seccode) {
		this.seccode = seccode; 
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	
}
